package com.note.simpleadmin.module.authc.service;

import com.note.simpleadmin.module.authc.dao.BgUserRepository;
import com.note.simpleadmin.entity.BgUser;
import com.note.simpleadmin.exception.DataUnanimousException;
import com.note.simpleadmin.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 *@ClaseName:BgUserService
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/27 0027 14:31
 */
@Service
public class BgUserService {
    @Autowired
    private BgUserRepository bgUserRepository;
    public BgUser findByUsername(String username)
    {
        return bgUserRepository.findByUsername(username);
    }
    public void updatePassword(String id,String oldPassword,String newPassword) throws DataUnanimousException
    {
        BgUser bgUser=bgUserRepository.getOne(id);
        if(bgUser.getPwd().equals( PasswordUtils.encode(oldPassword,bgUser.getSalt()))) {
            bgUser.setPwd(PasswordUtils.encode(newPassword,bgUser.getSalt()));
            bgUserRepository.save(bgUser);
        }
        else {
            throw new DataUnanimousException("密码错误");
        }
    }

}
